package org.wak.gui.modern
{
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	import flash.ui.Mouse;
	
	import org.wak.character.AdventureCharacter;
	import org.wak.gui.IAdventureGui;
	import org.wak.gui.events.GuiEvent;
	import org.wak.main.AdventureMaster;
	import org.wak.utils.WakSimpleButton;
	

	/**
	 * Modern Gui class. For the modern adventure style. 
	 * 
	 */
	public class ModernGui extends Sprite implements IAdventureGui
	{
		
		protected var mResources:Class = null ;
		
		protected var mMouseCursor:Sprite = null ;
		
		protected var mEnabled:Boolean = true ;
		
		protected var mCurrentMouseCursorId:String = "" ;
		
		protected var mPrevMouseCursorId:String = "" ;
		
		// Asoc Array id to mouse cursor DisplayObject.
		protected var mMouseCursorsArr:Array = null ;
		
		protected var mMouseInfo:MouseInfoBox = null ;
		
		protected var mInfoBox:MouseInfoBox = null ;
		
		protected var mInventory:ModernInventoryDisplay = null ;
		
		// local ref to the player character.
		protected var mPlayerChar:AdventureCharacter = null ;
		
		protected var mItemVisualHelp:Shape = null ;
		////////////////////
		
		// mouse cursors id.
		public static var ItemMouseCursor:String = "item_cursor" ;
		public static var CharMouseCursor:String = "char_cursor" ;
		public static var ExitMouseCursor:String = "exit_cursor" ;
		public static var NormalMouseCursor:String = "normal_cursor" ;
		public static var GrabingItemMouseCursor:String = "grabitem_cursor" ;
		
		// setings var.
		
		// Default normal mouse cursor.
		protected var mDefaultMouseCursor:Sprite = null ;
		
		// Mouse cursor for mouse over items.
		protected var mItemOverMouseCursor:Sprite = null ;
		
		// Mouse cursor for mouse over characters.
		protected var mCharOverMouseCursor:Sprite = null ;
		
		//  Mouse cursor for mouse over gatespots.
		protected var mGatespotOverMouseCursor:Sprite = null ;
		
		// mouse info config vars.
		protected var mMouseInfoEnabled:Boolean = true ;
		
		protected var mMouseInfoWidth:Number = 0 ;
		
		protected var mMouseInfoBackgroundId:String = "" ;
		
		protected var mMouseInfoFont:String  = "" ;
		
		protected var mMouseInfoFontSize:int = 14 ;
		
		protected var mMouseInfoFontColor:uint = 0xFFFFFF ;
		
		protected var mMouseInfoFontBold:Boolean = false ;
		
		protected var mMouseInfoFontUseEmbed:Boolean = false ;
		
		protected var mMouseInfoOutlineEnabled:Boolean = true ;
		
		protected var mMouseInfoFlatBgColor:uint = 0x000000 ;
		protected var mMouseInfoFlatBorderColor:uint = 0x000000 ;
		
		protected var mMouseInfoDescriptionEnabled:Boolean = false ;
		
		// Inventory config vars.-------------------------------------
		
		protected var mInventoryX:Number = 0 ;
		protected var mInventoryY:Number = 300 ;
		
		// horizontal space between inventory items.
		protected var mInventoryItemSpace:Number = 10 ;
		
		protected var mInventoryItemW:Number = 50 ;
		protected var mInventoryItemH:Number = 50 ;
		
		protected var mInventoryItemsY:Number = 1 ;
		
		protected var mInventoryColumns:int = 4 ;
		
		protected var mInventoryBg:DisplayObject = null ;
		
		protected var mInventoryArrowLeft:WakSimpleButton = null ;
		protected var mInventoryArrowRight:WakSimpleButton = null ;
		
		
		protected const kInventoryArrowLeft:String = "ScrollLeft" ;
		protected const kInventoryArrowRight:String = "ScrollRight" ;
		
		// mouse info settings ----------------------
		
		protected var mInfoBoxWidth:Number = 0 ;
		
		protected var mInfoBoxBackgroundId:String = "" ;
		
		protected var mInfoBoxFont:String  = "" ;
		
		protected var mInfoBoxFontSize:int = 14 ;
		
		protected var mInfoBoxFontColor:uint = 0xFFFFFF ;
		
		protected var mInfoBoxFontBold:Boolean = false ;
		
		protected var mInfoBoxFontUseEmbed:Boolean = false ;
		
		protected var mInfoBoxOutlineEnabled:Boolean = true ;
		
		protected var mInfoBoxFlatBgColor:uint = 0x000000 ;
		protected var mInfoBoxFlatBorderColor:uint = 0x000000 ;
		
		protected var mInfoBoxDescriptionEnabled:Boolean = false ;
		
		protected var mInfoBoxLeading:int = 0 ;
		
		protected var mInfoBoxEnabled:Boolean = false ;
		
		/**
		 * Constructor 
		 * @param pGuiSettings XML Settings for the gui.
		 * 
		 */		
		public function ModernGui(pGuiSettings:XML = null)
		{
			super();
			mResources = AdventureMaster.Resources ;
			mMouseCursorsArr = new Array();
			mMouseInfo = new MouseInfoBox();
			mInfoBox = new MouseInfoBox();
			LoadSettings(pGuiSettings);
			mMouseCursor = mDefaultMouseCursor ;
			mCurrentMouseCursorId = NormalMouseCursor ;
			
			mPlayerChar = AdventureMaster.CharactersMngr.GetCharacter( AdventureMaster.defaultPlayerCharacterId ) ;
			mInventory = new ModernInventoryDisplay( mPlayerChar.inventory, mInventoryColumns, mInventoryItemW, mInventoryItemH);
						
			mInventory.x = mInventoryX ;
			mInventory.y = mInventoryY ;
			mInventory.itemSeparatorSpace = mInventoryItemSpace ;
			mInventory.background = mInventoryBg ;
			mInventory.arrowLeft = mInventoryArrowLeft ;
			mInventory.arrowRight = mInventoryArrowRight ;
			//trace("mInventoryItemsY:"+mInventoryItemsY);
			mInventory.itemsY = mInventoryItemsY ;
			
			mItemVisualHelp = CreateItemVisualHelp() ;
			
			this.addEventListener(Event.ADDED_TO_STAGE,OnAddedToStage);
		}
		
		protected function CreateItemVisualHelp():Shape
		{
			var fHelp:Shape = new Shape();
			fHelp.graphics.beginFill(0x000000);
			fHelp.graphics.drawRect(-1,-1,2,2);
			fHelp.graphics.endFill() ;
			
			fHelp.graphics.beginFill(0xFFFFFF);
			fHelp.graphics.drawRect(0,0,1,1);
			fHelp.graphics.endFill() ;
			return fHelp ;
		}
		
		protected function LoadNormalCursor(pSettings:XML):void
		{
			var fCursor:DisplayObject = null ;
			var fCursorResId:String = "";
			
			if( pSettings.DefaultMouseCursor != "" )
			{
				fCursorResId = pSettings.DefaultMouseCursor ;
				
				fCursor =  new mResources[fCursorResId] ;
				
				if( pSettings.DefaultMouseCursor.@x != "" )
					fCursor.x = pSettings.DefaultMouseCursor.@x ;
					
				if( pSettings.DefaultMouseCursor.@y != "" )
					fCursor.y = pSettings.DefaultMouseCursor.@y ;
				
				
				mDefaultMouseCursor.addChild( fCursor);
				
			}
			else
			{
				fCursor = CreateDefaultCursor();
				mDefaultMouseCursor.addChild(fCursor);
			}
			mMouseCursorsArr[ NormalMouseCursor ] = fCursor ;
		}
		
		protected function LoadMouseOverItemCursor( pSettings:XML ):void
		{
			var fCursor:DisplayObject = null ;
			var fCursorResId:String = "";
			
			if( pSettings.ItemOverMouseCursor != "" )
			{
				fCursorResId = pSettings.ItemOverMouseCursor ;
				
				fCursor =  new mResources[fCursorResId] ;
				
				if( pSettings.ItemOverMouseCursor.@x != "" )
					fCursor.x = pSettings.ItemOverMouseCursor.@x ;
					
				if( pSettings.ItemOverMouseCursor.@y != "" )
					fCursor.y = pSettings.ItemOverMouseCursor.@y ;
				
				mItemOverMouseCursor.addChild( fCursor);
				
			}
			else
			{
				fCursor = CreateDefaultItemCursor();
				mItemOverMouseCursor.addChild(fCursor);
			}
			mMouseCursorsArr[ ItemMouseCursor ] = fCursor ;
		}
		
		protected function LoadMouseOverChar(pSettings:XML):void
		{
			var fCursor:DisplayObject = null ;
			var fCursorResId:String = "";
			
			if( pSettings.CharacterOverMouseCursor != "" )
			{
				fCursorResId = pSettings.CharacterOverMouseCursor ;
				
				fCursor =  new mResources[fCursorResId] ;
				
				if( pSettings.CharacterOverMouseCursor.@x != "" )
					fCursor.x = pSettings.CharacterOverMouseCursor.@x ;
					
				if( pSettings.CharacterOverMouseCursor.@y != "" )
					fCursor.y = pSettings.CharacterOverMouseCursor.@y ;
				
				
				mCharOverMouseCursor.addChild( fCursor);
				
			}
			else
			{
				fCursor = CreateDefaultItemCursor();
				mCharOverMouseCursor.addChild(fCursor);
			}
			mMouseCursorsArr[ CharMouseCursor ] = fCursor ;
		}
		
		protected function LoadMouseOverGatespot(pSettings:XML):void
		{
			var fCursor:DisplayObject = null ;
			var fCursorResId:String = "";
			
			if( pSettings.GatespotOverMouseCursor != "" )
			{
				fCursorResId = pSettings.GatespotOverMouseCursor ;
				
				fCursor =  new mResources[fCursorResId] ;
				
				if( pSettings.GatespotOverMouseCursor.@x != "" )
					fCursor.x = pSettings.GatespotOverMouseCursor.@x ;
					
				if( pSettings.GatespotOverMouseCursor.@y != "" )
					fCursor.y = pSettings.GatespotOverMouseCursor.@y ;
				
				mGatespotOverMouseCursor.addChild( fCursor);
				
			}
			else
			{
				fCursor = CreateDefaultItemCursor();
				mGatespotOverMouseCursor.addChild(fCursor);
			}
			mMouseCursorsArr[ ExitMouseCursor ] = fCursor ;
		}
		
		protected function LoadMouseInfoSettings(pSettings:XML):void
		{
			
				mMouseInfoBackgroundId = pSettings.MouseInfo.Background ;
				if( mMouseInfoBackgroundId != "" )
				{
					mMouseInfo.background = new mResources[mMouseInfoBackgroundId] ;
				}
				else
				{
					if( pSettings.MouseInfo.FlatBgColor != "")
					{
						mMouseInfoFlatBgColor = uint("0x"+pSettings.MouseInfo.FlatBgColor) ;
						mMouseInfo.flatBgColor = mMouseInfoFlatBgColor ;
					} 
						
					if( pSettings.MouseInfo.FlatBgBorder != "")
					{
						mMouseInfoFlatBorderColor = uint("0x"+pSettings.MouseInfo.FlatBgBorder) ;
						mMouseInfo.border = mMouseInfoFlatBorderColor ;
					}
						
				}
				
				
				if( pSettings.MouseInfo.Width != "")
					mMouseInfoWidth = pSettings.MouseInfo.Width ;
				
				if( pSettings.MouseInfo.Font != "")
					mMouseInfoFont = pSettings.MouseInfo.Font ;
					
				if( pSettings.MouseInfo.FontSize != "" )
					mMouseInfoFontSize = pSettings.MouseInfo.FontSize ;
				
				if( pSettings.MouseInfo.FontColor != "")
					mMouseInfoFontColor = uint("0x"+pSettings.MouseInfo.FontColor ); 	
					
				
					
				mMouseInfoFontBold = pSettings.MouseInfo.Bold == "true" ? true : false ;
				
				mMouseInfoFontUseEmbed = pSettings.MouseInfo.UseEmbedFont == "true" ? true : false ;
				mMouseInfoOutlineEnabled = pSettings.MouseInfo.UseOutline == "true" ? true : false ;
				
				mMouseInfo.embedFonts = mMouseInfoFontUseEmbed ;
				mMouseInfo.outline = mMouseInfoOutlineEnabled ;
				
				var fInfoFormat:TextFormat = new TextFormat();
				fInfoFormat.font = mMouseInfoFont ;
				fInfoFormat.color = mMouseInfoFontColor ;
				fInfoFormat.bold = mMouseInfoFontBold ;
				fInfoFormat.size = mMouseInfoFontSize ;
				mMouseInfo.textFormat = fInfoFormat ;
				
				if( mMouseInfoWidth != 0 )
					mMouseInfo.txtFieldWidth = mMouseInfoWidth ; 
		}
		
		protected function LoadInventorySettings(pSettings:XML):void
		{
			
			// Inventory Position	
			if(pSettings.Inventory.Position.@x != "")
				mInventoryX = pSettings.Inventory.Position.@x ; 
			
			if(pSettings.Inventory.Position.@y != "")
				mInventoryY = pSettings.Inventory.Position.@y ; 
				
			// Items Width and Height
			if(pSettings.Inventory.ItemWidth != "")
				mInventoryItemW = pSettings.Inventory.ItemWidth ;
				
			if(pSettings.Inventory.ItemHeight != "")
				mInventoryItemW = pSettings.Inventory.ItemHeight ;
				
			// Item space
			if(pSettings.Inventory.ItemSpace != "")
				mInventoryItemSpace = pSettings.Inventory.ItemSpace ;
			//trace("mInventoryItemSpace: "+mInventoryItemSpace);
			
			// Background
			var fBgId:String = "";
			if(pSettings.Inventory.Background != "")
			{
				fBgId = pSettings.Inventory.Background ;
				mInventoryBg = new mResources[fBgId] ;
				
				if(pSettings.Inventory.Background.@x != "")
					mInventoryBg.x = pSettings.Inventory.Background.@x ;
					
				if(pSettings.Inventory.Background.@y != "")
					mInventoryBg.y = pSettings.Inventory.Background.@y ;
			}
			
			if( pSettings.Inventory.ItemsY != "")
				mInventoryItemsY = pSettings.Inventory.ItemsY ;
			
			// item columns.
			if( pSettings.Inventory.Cols != "" )
				mInventoryColumns = pSettings.Inventory.Cols ;
			
			// Inventory buttons.
			for each( var button:XML in pSettings.Inventory.Button )
			{
				if(button.@id == kInventoryArrowLeft)
				{
					mInventoryArrowLeft = CreateInventoryButton(button);
				}
				else if(button.@id == kInventoryArrowRight)
				{
					mInventoryArrowRight = CreateInventoryButton(button);
				}
			}
		}
		
		protected function LoadSettings(pSettings:XML):void
		{
			
			var fCursor:DisplayObject = null ;
			var fCursorResId:String = "";
			
			mDefaultMouseCursor = new Sprite();
			// Load normal mouse cursor.
			LoadNormalCursor(pSettings) ;
			
			// Load item mouse over cursor.
			mItemOverMouseCursor = new Sprite();
			LoadMouseOverItemCursor(pSettings);
			
			
			// Load char mouse over cursor.
			mCharOverMouseCursor = new Sprite();
			LoadMouseOverChar(pSettings);
			
			// Load gatespot mouse over cursor.
			mGatespotOverMouseCursor = new Sprite();
			LoadMouseOverGatespot(pSettings);
			
			// Load MouseInfo settings.
			mMouseInfoEnabled = pSettings.MouseInfo.@visible == "true" ? true : false ;
			mMouseInfoDescriptionEnabled = pSettings.MouseInfo.@description == "true" ? true : false ;
			
			if(mMouseInfoEnabled)
			{
				LoadMouseInfoSettings(pSettings);
			}
			
			/// Inventory Settings.
			LoadInventorySettings(pSettings);
			
			// InfoBox Settings.
			mInfoBoxEnabled = pSettings.InfoPanel.@visible == "true" ? true : false ;
			if(mInfoBoxEnabled)
			{
				mInfoBoxBackgroundId = pSettings.InfoPanel.Background ;
				if( mInfoBoxBackgroundId != "" )
				{
					mInfoBox.background = new mResources[mInfoBoxBackgroundId] ;
				}
				else
				{
					if( pSettings.InfoPanel.FlatBgColor != "")
					{
						mInfoBoxFlatBgColor = uint("0x"+pSettings.InfoPanel.FlatBgColor) ;
						mInfoBox.flatBgColor = mInfoBoxFlatBgColor ;
					} 
						
					if( pSettings.InfoPanel.FlatBgBorder != "")
					{
						mInfoBoxFlatBorderColor = uint("0x"+pSettings.InfoPanel.FlatBgBorder) ;
						mInfoBox.border = mInfoBoxFlatBorderColor ;
					}
						
				}
				
				
				if( pSettings.InfoPanel.Width != "")
					mInfoBoxWidth = pSettings.InfoPanel.Width ;
				
				if( pSettings.InfoPanel.Font != "")
					mInfoBoxFont = pSettings.InfoPanel.Font ;
					
				if( pSettings.InfoPanel.FontSize != "" )
					mInfoBoxFontSize = pSettings.InfoPanel.FontSize ;
				
				if( pSettings.InfoPanel.FontColor != "")
					mInfoBoxFontColor = uint("0x"+pSettings.InfoPanel.FontColor ); 	
					
				
					
				mInfoBoxFontBold = pSettings.InfoPanel.Bold == "true" ? true : false ;
				
				mInfoBoxFontUseEmbed = pSettings.InfoPanel.UseEmbedFont == "true" ? true : false ;
				mInfoBoxOutlineEnabled = pSettings.InfoPanel.UseOutline == "true" ? true : false ;
				
				mInfoBox.embedFonts = mInfoBoxFontUseEmbed ;
				mInfoBox.outline = mInfoBoxOutlineEnabled ;
				
				if( pSettings.InfoPanel.Leading != "" )	
					mInfoBoxLeading = pSettings.InfoPanel.Leading ;
				
				var fInfoBoxFormat:TextFormat = new TextFormat();
				fInfoBoxFormat.font = mInfoBoxFont ;
				fInfoBoxFormat.color = mInfoBoxFontColor ;
				fInfoBoxFormat.bold = mInfoBoxFontBold ;
				fInfoBoxFormat.size = mInfoBoxFontSize ;
				fInfoBoxFormat.align = TextFormatAlign.CENTER ;
				fInfoBoxFormat.kerning = true ;
				fInfoBoxFormat.leading = mInfoBoxLeading ;
				mInfoBox.textFormat = fInfoBoxFormat ;
				
				if( mInfoBoxWidth != 0 )
					mInfoBox.txtFieldWidth = mInfoBoxWidth ;
					
				if( pSettings.InfoPanel.Position.@x != "" )	
					mInfoBox.x = pSettings.InfoPanel.Position.@x ;
					
				if( pSettings.InfoPanel.Position.@y != "" )	
					mInfoBox.y = pSettings.InfoPanel.Position.@y ;
				
				
			}
		
			 
		}
		
		protected function CreateInventoryButton(pBtn:XML):WakSimpleButton
		{
			if((pBtn.Up == "") || (pBtn.Over == "") || (pBtn.Down == "") )
			{
				return null ;
			}
			
			var fRetBtn:WakSimpleButton = null ;
			
			var fUp:DisplayObject = new mResources[pBtn.Up]  ;
			var fOver:DisplayObject = new mResources[pBtn.Over]  ;
			var fDown:DisplayObject = new mResources[pBtn.Down]  ;
			
			fRetBtn = new WakSimpleButton(fUp, fOver, fDown) ;
			
			return fRetBtn; 
		}
		
		
		protected function CreateDefaultCursor():DisplayObject
		{
			var fRet:Shape = new Shape();
			
			
			fRet.graphics.beginFill(0xFFFFFF);
			fRet.graphics.drawCircle(0,0,3);
			fRet.graphics.endFill() ;
			
			fRet.graphics.beginFill(0x000000);
			fRet.graphics.drawCircle(0,0,2);
			fRet.graphics.endFill() ;
			return fRet ;
		}
		
		protected function CreateDefaultItemCursor():DisplayObject
		{
			var fRet:Shape = new Shape();
			
			
			fRet.graphics.beginFill(0xFFFFFF);
			fRet.graphics.drawCircle(0,0,6);
			fRet.graphics.endFill() ;
			
			fRet.graphics.beginFill(0x000000);
			fRet.graphics.drawCircle(0,0,2);
			fRet.graphics.endFill() ;
			return fRet ;
		}
		
		protected function OnAddedToStage(event:Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE,OnAddedToStage);
			stage.addEventListener(MouseEvent.MOUSE_MOVE,OnMouseMove );
			
			if(!this.contains( mInventory ) )
			{
				addChild( mInventory );
			}
			SetInventoryDisplayListeners( mInventory );
			
			if( mInfoBoxEnabled )
			{
				if(!this.contains(mInfoBox) )
				{
					addChild(mInfoBox);
				}
			}
			
			this.addEventListener(Event.REMOVED_FROM_STAGE, OnRemovedFromStage);
		}
		
		protected function OnRemovedFromStage(event:Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE,OnAddedToStage);
			RemoveInventoryDisplayListeners( mInventory );
			stage.removeEventListener(MouseEvent.MOUSE_MOVE,OnMouseMove );
		}
		
		protected function OnMouseMove(event:MouseEvent):void
		{
			mMouseCursor.x = event.stageX ;
			mMouseCursor.y = event.stageY ;
			
			if( mMouseInfoEnabled )
			{
				if( (event.stageX + mMouseInfo.width) < this.stage.stageWidth  )
				{
					mMouseInfo.x = event.stageX + mMouseCursor.width ;
				}
				else
				{
					mMouseInfo.x = event.stageX - mMouseInfo.width ;
				}
				
				mMouseInfo.y = event.stageY ;
			}
			
			if(!mEnabled)
				return ;
		}
		
		public function SetMouseInfoText(value:String):void
		{
			if(!mMouseInfoEnabled)
				return ;
			mMouseInfo.text = value ;
		}
		
		protected function SetInventoryDisplayListeners( pInvDisplay:ModernInventoryDisplay ):void
		{
			pInvDisplay.addEventListener(GuiEvent.INVENTORY_ITEM_OUT, OnInventoryItemOut );
			pInvDisplay.addEventListener(GuiEvent.INVENTORY_ITEM_OVER, OnInventoryItemOver );
			pInvDisplay.addEventListener(GuiEvent.INVENTORY_ITEM_SELECTED, OnInventoryItemClick );
		}
		
		protected function RemoveInventoryDisplayListeners( pInvDisplay:ModernInventoryDisplay ):void
		{
			pInvDisplay.removeEventListener(GuiEvent.INVENTORY_ITEM_OUT, OnInventoryItemOut );
			pInvDisplay.removeEventListener(GuiEvent.INVENTORY_ITEM_OVER, OnInventoryItemOver );
			pInvDisplay.removeEventListener(GuiEvent.INVENTORY_ITEM_SELECTED, OnInventoryItemClick );
		}
		
		private function OnInventoryItemOver(event:GuiEvent):void
		{
			if(!mEnabled)
				return ;
				
			var fEvent:GuiEvent = new GuiEvent(event.type,"",event.inventoryItem);
			this.dispatchEvent( fEvent );
		}
		
		private function OnInventoryItemOut(event:GuiEvent):void
		{
			if(!mEnabled)
				return ;
				
			var fEvent:GuiEvent = new GuiEvent(event.type,"",event.inventoryItem);
			this.dispatchEvent( fEvent );
		}
		
		private function OnInventoryItemClick(event:GuiEvent):void
		{
			if(!mEnabled)
				return ;
				
			var fEvent:GuiEvent = new GuiEvent(event.type,"",event.inventoryItem);
			this.dispatchEvent( fEvent );
		}
		
		
		/**
		 * Change the current mouse cursor to other mouse cursor state.
		 * Valid mouse cursors id are the ModernGui vars : ItemMouseCursor, CharMouseCursor,
		 *  ExitMouseCursor ,  NormalMouseCursor .
		 * @param pCursorId an MouseCursor id see ModernGui constants.
		 * @see ModernGui
		 * 
		 */		
		public function ChangeMouseCursorTo(pCursorId:String):void
		{
			if(mCurrentMouseCursorId == GrabingItemMouseCursor)
				return ;
			
			var fCurrentCursor:DisplayObject = mMouseCursorsArr[mCurrentMouseCursorId] as DisplayObject ;
			fCurrentCursor.visible = false ;
			mPrevMouseCursorId = mCurrentMouseCursorId ;
			mCurrentMouseCursorId = pCursorId ;
			
			var fNextCursor:DisplayObject = mMouseCursorsArr[pCursorId] as DisplayObject ;
			fNextCursor.visible = true ;
			
			if(!mMouseCursor.contains( fNextCursor ) )
				mMouseCursor.addChild( fNextCursor );
				
			if(mCurrentMouseCursorId == GrabingItemMouseCursor)
			{
				mMouseCursor.addChild( mItemVisualHelp );
			}
			else
			{
				if( mMouseCursor.contains( mItemVisualHelp ) )
					mMouseCursor.removeChild( mItemVisualHelp );
			}
			
		}
		
		public function get prevMouseCursorId():String
		{
			return mPrevMouseCursorId ;
		}
		
		/**
		 * @inheritDoc 
		 */		
		public function get display():DisplayObject
		{
			return this ;
		}
		
		/**
		 * @inheritDoc 
		 */		
		public function SetMouseCursor(pCursorLayer:Sprite):void
		{
			Mouse.hide() ;
			pCursorLayer.addChild(mMouseCursor);
			
			if(mMouseInfoEnabled)
			{
				mMouseInfo.visible = false
				pCursorLayer.addChild(mMouseInfo);
			}
		}
		
		/**
		 * @inheritDoc 
		 */		
		public function RemoveMouseCursor(pCursorLayer:Sprite):void
		{
			Mouse.show() ;
			if(pCursorLayer.contains(mMouseCursor) )
				pCursorLayer.addChild(mMouseCursor);
			
			if(mMouseInfoEnabled)
			{
				mMouseInfo.visible = false;
				if(pCursorLayer.contains(mMouseInfo) )
					pCursorLayer.addChild(mMouseInfo);
			}
		}
		
		/**
		 * @inheritDoc 
		 */		
		public function set enabled(value:Boolean):void
		{
			mEnabled = value ;
			if(mEnabled)
			{
				this.visible = true;
				mMouseInfo.visible = true ;
				mMouseInfo.text = "";
			}
			else
			{
				this.visible = false;
				mMouseInfo.visible = false ;
			}
		}
		
		/**
		 * @inheritDoc 
		 */		
		public function get enabled():Boolean
		{
			return mEnabled;
		}
		
		/**
		 * @inheritDoc 
		 */		
		public function get mouseCursor():DisplayObject
		{
			return mMouseCursor;
		}
		
		/**
		 *  
		 * @return true if the Scene item description is enabled. 
		 * 
		 */		
		public function get mouseInfoDescription():Boolean
		{
			return mMouseInfoDescriptionEnabled ;
		}
		
		/**
		 * Set text into the gui info box. 
		 * @param value String with the text to set.
		 * 
		 */		
		public function SetInfoBoxText(value:String):void
		{
			if( !mInfoBoxEnabled )
				return ;
				
			mInfoBox.text = value ;
		}
		
		/**
		 * Change the mouse cursor to grab an item. 
		 * @param pItem Mouse icon DisplayObject.
		 * 
		 */		
		public function GrabItem(pItem:DisplayObject):void
		{
			mMouseCursorsArr[GrabingItemMouseCursor] = pItem ;
			ChangeMouseCursorTo(GrabingItemMouseCursor);
		}
		
		/**
		 * Release the previously grabbed items. 
		 * 
		 */		
		public function ReleaseItem():void
		{
			var fItem:DisplayObject = mMouseCursorsArr[GrabingItemMouseCursor] ;
			if(fItem != null)
			{
				if(mMouseCursor.contains(fItem))
					mMouseCursor.removeChild(fItem);
			}
			
			mPrevMouseCursorId = mCurrentMouseCursorId ;
			mCurrentMouseCursorId = NormalMouseCursor ;
			ChangeMouseCursorTo(mCurrentMouseCursorId);
		}
		
		public function get currentMouseCursorId():String
		{
			return mCurrentMouseCursorId ;
		}
		
	}
}
